package com.xhwl.data.service.impl;


import com.google.common.collect.Lists;
import com.xhwl.common.dto.cent.sdata.PermissionDTO;
import com.xhwl.common.pojo.cent.sdata.SdataAccountQuickEntry;
import com.xhwl.common.utils.ChkUtil;
import com.xhwl.data.dao.SdataAccountQuickEntryDao;
import com.xhwl.data.service.IPermissionQuickEntryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author:
 * @create: 2020-11-25
 **/
@Service
@Slf4j
public class PermissionQuickEntryServiceImpl implements IPermissionQuickEntryService {

    @Autowired
    SdataAccountQuickEntryDao sdataAccountQuickEntryDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Integer projectId, Integer uid, List<Integer> list) {
        sdataAccountQuickEntryDao.deleteByList(projectId, uid);
        List<SdataAccountQuickEntry> inserts = Lists.newLinkedList();
        for (int i = 0; i < list.size(); i++) {
            SdataAccountQuickEntry sdataAccountQuickEntry = new SdataAccountQuickEntry();
            sdataAccountQuickEntry.setMenuId(list.get(i));
            sdataAccountQuickEntry.setProjectId(projectId);
            sdataAccountQuickEntry.setUserId(uid);
            sdataAccountQuickEntry.setSort(i + 1);
            inserts.add(sdataAccountQuickEntry);
        }
        sdataAccountQuickEntryDao.insertByList(inserts);
    }

    @Override
    public List<PermissionDTO> list(Integer projectId, Integer uid) {
        List<PermissionDTO> permissionDTOS = sdataAccountQuickEntryDao.selectList(uid, projectId);
        if (ChkUtil.isNull(permissionDTOS)) {
            permissionDTOS = Lists.newArrayList();
        }
        return permissionDTOS;
    }

    @Override
    public List<PermissionDTO> menuTile(List<PermissionDTO> results, String keyword, List<Integer> excludeIds) {

        List<PermissionDTO> list = Lists.newLinkedList();
        //获取所有的末级菜单
        getEndMen(results, list);
        //排除一部分
        if (ChkUtil.isNotNull(excludeIds)) {
            list.removeIf(permissionDTO -> excludeIds.contains(permissionDTO.getId()));
        }
        //排除快捷登录的按钮
        List<PermissionDTO> quickList = threeMenuTile(results);
        if (ChkUtil.isNotNull(quickList)) {
            List<Integer> quickIdList = quickList.stream().map(PermissionDTO::getId).collect(Collectors.toList());
            list.removeIf(permissionDTO -> quickIdList.contains(permissionDTO.getId()));
        }
        //平铺
        if (ChkUtil.isNotNull(keyword)) {
            List<PermissionDTO> newLinkedList = Lists.newLinkedList();
            for (PermissionDTO permissionDTO : list) {
                if (permissionDTO.getName().contains(keyword)) {
                    newLinkedList.add(permissionDTO);
                }
            }
            list = newLinkedList;
        }
        return list;
    }

    /**
     * 获取所有快捷登录的菜单ID
     */
    @Override
    public List<PermissionDTO> threeMenuTile(List<PermissionDTO> res) {
        List<PermissionDTO> quickList = Lists.newLinkedList();
        for (PermissionDTO ret : res) {
            if ("home".equals(ret.getCode())) {
                List<PermissionDTO> sons = ret.getChildren();
                if (ChkUtil.isNotNull(sons)) {
                    for (PermissionDTO son : sons) {
                        if ("quickEntry".equals(son.getCode())) {
                            List<PermissionDTO> suns = son.getChildren();
                            if (ChkUtil.isNotNull(suns)) {
                                for (PermissionDTO sun : suns) {
                                    sun.setChildren(null);
                                    quickList.add(sun);
                                }
                            }
                            break;
                        }
                    }
                }
                break;
            }
        }
        return quickList;
    }

    /**
     * 递归平铺
     */
    private void getEndMen(List<PermissionDTO> res, List<PermissionDTO> list) {
        for (PermissionDTO ret : res) {
            if (ChkUtil.isNull(ret.getChildren())) {
                ret.setChildren(null);
                list.add(ret);
            } else {
                if (checkIsButton(ret.getChildren())) {
                    ret.setChildren(null);
                    list.add(ret);
                } else {
                    getEndMen(ret.getChildren(), list);
                }
            }
        }
    }

    private boolean checkIsButton(List<PermissionDTO> res) {
        boolean isBtn = true;
        for (PermissionDTO ret : res) {
            if (ret.getMenuType() == 1) {
                isBtn = false;
            }
        }
        return isBtn;
    }
}
